import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.io.IOException;
import java.lang.Thread;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.commons.cli.*;

public class myproxy{
	public InputStream ServerSideInput, ClientSideInput;
	public OutputStream ServerSideOutput, ClientSideOutput;
	
	public class commandLineProxy{
		private String[] args;
		public Options options;
		private boolean OptionsConfigure = false;
		private HelpFormatter PrinterFormatter = null;
		private int lport = 2000;
		private String remotIP = "localhost";
		private int rport = 23;
		private CommandLineParser Parser;
		
		public commandLineProxy(String[] argumentos){
			args = argumentos;
			options = new Options();
			PrinterFormatter = new HelpFormatter();
			Parser = new PosixParser();
			OptionsCreator();
		}
		
		private void OptionsCreator(){
			OptionsConfigure = true;
			options.addOption("h", "help", false, "Muestra la ayuda");
			options.addOption("l", "lport", true, "puerto local donde poner a escuchar el programa");
			options.addOption("r", "remotIP", true, "IP remota a donde se conectara el proxy");
			options.addOption("p", "rport", true, "Puerto remoto donde nos conetaremos");
		}
		
		public void PrintCmdLineHelp(){
			PrinterFormatter.printHelp("myproxy", options);
		}
		
		public void CmdAnalisis(){
			Option[] tmpArray = null;
			CommandLine cmd = null;
			try{
				cmd = Parser.parse(options, args);
			}
			catch(ParseException e){
				System.err.format("Error al manejar la linea de comandos %s%n", e.getMessage());
			}
			tmpArray = cmd.getOptions();
			//System.out.format("Tamano %d tiene%n", tmpArray.length, tmpArray);
			for(int i = 0; i < tmpArray.length; i++){
				switch(tmpArray[i].getOpt()){
				case "h":
					PrintCmdLineHelp();
					System.exit(1);
				case "l":
					lport = Integer.parseInt(tmpArray[i].getValue());
					break;
				case "r":
					remotIP = tmpArray[i].getValue();
					break;
				case "p":
					rport = Integer.parseInt(tmpArray[i].getValue());
					break;
				default:
					System.out.println("Comando invalido " + tmpArray[i].getArgName());
				}
			}
		}
		public int getLocalPort(){
			return lport;
		}
		
		public int getRemotePort(){
			return rport;
		}
		
		public String getRemoteIP(){
			return remotIP;
		}
	}

	public class ProxyServer{
		public int localport;
		private ServerSocket socketserver = null;
		
		public ProxyServer(int localport){
			this.localport = localport;
		}
		
		public void CreateListenner() throws IOException{
			try{
				System.out.println("myproxy");
				socketserver = new ServerSocket(localport);
			}
			catch(IOException e){
				System.err.println("Imposible crear el socket de escucha " + e.getMessage());
			}
		}
	}
	
	public class ProxyConnectionClient{
		public String remoteip;
		public int remoteport;
		private Socket OutPutConnection = null;
		public ProxyConnectionClient(String remoteip, int remoteport){
			this.remoteip = remoteip;
			this.remoteport = remoteport;
		}
		
		public void CreateConnector() throws UnknownHostException, IOException{
			System.out.println("Generando conexion a " + remoteip);
			OutPutConnection = new Socket(remoteip, remoteport);
		}
	}
	
	public static void main(String[] args) throws IOException{
		myproxy px = new myproxy();
		myproxy.commandLineProxy cmdline = px.new commandLineProxy(args);
		if(args.length <= 1){
			cmdline.PrintCmdLineHelp();
		}
		cmdline.CmdAnalisis();
		myproxy.ProxyServer proxyserver = px.new ProxyServer(cmdline.getLocalPort());
		proxyserver.CreateListenner();
		myproxy.ProxyConnectionClient proxyconnectionclient = px.new ProxyConnectionClient(cmdline.getRemoteIP(), cmdline.getRemotePort());
		try{
			proxyconnectionclient.CreateConnector();
		}
		catch(UnknownHostException e){
			System.err.println("Se genero un error con el host: " + e.getMessage());
		}
		catch(IOException e){
			System.err.println("Se genero un error con la entrada y saldia de flujos: " + e.getMessage());
		}
		System.out.println("Hola Mundo");
	}
}
